/*
  	Arkodroid - An Arduino Controller for Android
	Copyright (c) 2013 Miguel Monteiro, Eduardo Peixoto, Rui Pedro Carvalhal, André Braga.
	All rights reserved.
	http://code.google.com/p/arkodroid/
	
	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package net.miguelmonteiro.android.arkodroid;

import java.util.Arrays;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.DialogInterface;

public class Checker {

        /* FIELDS */

        private Activity activity;
        private Pass pass;
        private List<Resource> resourcesList;

        /* TYPES */

        public enum Resource {
                BLUETOOTH
        }

        public static abstract class Pass {
                public abstract void pass();
        }

        /* API */

        public Checker(Activity activity) {
                this.activity = activity;
        }

        public void check(Resource... resources) {
                resourcesList = Arrays.asList(resources);
                if (resourcesList.contains(Resource.BLUETOOTH) && !isBluetoothActivated(activity)) {
                        new AlertDialog.Builder(activity)
                    	.setIcon(android.R.drawable.ic_dialog_alert)
                        .setTitle(R.string.checker_bluetooth_title)
                        .setMessage(R.string.checker_bluetooth_message)
                        .setCancelable(false)
                        .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                	BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                                	mBluetoothAdapter.enable();
                                }
                        }).setNegativeButton(R.string.exit, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                        dialog.cancel();
                                        activity.finish();
                                }
                        }).create().show();
                }else{
                   pass.pass();
                }
        }

        public Checker pass(Pass pass) {
                this.pass = pass;
                return this;
        }

        /* PRIVATE */
        
        private boolean isBluetoothActivated(Context context) {
            return BluetoothAdapter.getDefaultAdapter().isEnabled();
        }


}